# -*- coding: utf-8 -*-

import sys
import os
import pyRXP
import pprint
import time
import math

import re
import dotgraph

"""

[(10, -180), (10, -220), (172, -220), (172, -180)]
[(33, -108), (33, -144), (100, -144), (100, -108)]
[(189, -108), (189, -144), (255, -144), (255, -108)]
[(117, -108), (117, -144), (171, -144), (171, -108)]
[(72, -18), (72, -54), (138, -54), (138, -18)]
[(0, 0), (0, -72), (54, -72), (54, 0)]
[(190, -182), (190, -218), (255, -218), (255, -182)]

[(14, 44), (14, 4), (176, 4), (176, 44)]
[(37, 116), (37, 80), (104, 80), (104, 116)]
[(193, 116), (193, 80), (259, 80), (259, 116)]
[(121, 116), (121, 80), (175, 80), (175, 116)]
[(76, 206), (76, 170), (142, 170), (142, 206)]
[(4, 224), (4, 152), (58, 152), (58, 224)]
[(194, 42), (194, 6), (259, 6), (259, 42)]

"""
    
class DotSVG:

    def __init__(self, xml, ox=0, oy=0):
        cwd = os.getcwd()
        #self.sqlfile = open("wurfl.sql","w");
        self.graph = dotgraph.DotGraph()
        self.name = ""
        self.gtype = ""
        self.ox = ox
        self.oy = oy
        self.detranslate =  True
        try:
            #print 'BEGIN'
            t0 = time.time()
            #print cwd
            #xml = open(cwd+"\\"+fn,'r').read()
            #os.chdir(os.path.dirname(fn))
            t1 = time.time()
            p = pyRXP.Parser(TrustSDD = 0 )            
            self.D = p.parse(xml)
            
            t2 = time.time()  
            self.svg()
            #print 'END read took %7.2f" parse took %7.2f"' %(t1-t0,t2-t1)
            #print self.D

        finally:
            os.chdir(cwd)
        
    
    def getTransform(self):
        self.transform = self.D[2][1][1]['transform']
            
    def getTranslate(self):
        print self.transform
        trans =  self.transform.split(' ')
        rawstr = r"""translate\((\d+) (\d+)\)"""
        compile_obj = re.compile(rawstr)
        match_obj = compile_obj.search(self.transform)
        if match_obj:
            self.offset_x = int(match_obj.group(1))
            self.offset_y = int(match_obj.group(2))
            #print offset_x, offset_y
        
    def svg(self):        
        if self.D[0] =='svg':
            self.getTransform() 
            self.getTranslate()            
            for i in range(0, len(self.D[2][1][2])):
                
                self.ge(self.D[2][1][2][i])
            #print t2-t0
            
        else:
            print "not a svg file"
            
    
    def ge(self, info):
        if info[0] =='g':            
            if info[1]['class'] == 'node':
                #print ">>>>>>>>>>>>>>>>"
                self.gtype = "node"
            elif info[1]['class'] == 'edge':
                #print ">>>>>>>>>>>>>>>>"
                self.gtype = "edge"
            #print self.gtype
            for i in range(0, len(info[2])):                
                self.g(info[2][i])        
        
    def g(self, info):
        #print ">>>>>>>>>>>>>>>>"
        #print info
        if info[0] =='title':
            #print info[2][0]
            self.name = info[2][0]
            pass
        elif info[0] =='polygon' and self.gtype == "node":
            p =  info[1]['points']
            #print info[2][0]
            node = dotgraph.Node(self.name, self.polygon(p)[:4]) # for points for one rect node
            self.graph.addNode(node)
            #self.polygon(pl)
        elif info[0] =='path':
            d = info[1]['d']
            path = self.pathd(d)
            edge = dotgraph.Edge(self.name, path)
            self.graph.addEdge(edge)
            

 
    def polygon(self, pl):
        pstr = pl.split(" ")
        for i in range(0, len(pstr)):            
            pstr[i] = self.point(pstr[i])            
        return pstr
        
    def ellipse(self, info):
        #<ellipse style="fill: none; stroke: red;" cx="263" cy="-34" rx="29.2552" ry="29.6985"/>
        #cx, cy, rx, ry
        pass
        
    def point(self, pstr):
        item = eval("("+pstr+")") 
        if self.detranslate:
            point = (int(item[0]) + self.offset_x + self.ox,  int(item[1]) + self.offset_y + self.oy)
        else:
            point = (int(item[0]) + self.ox,  int(item[1])+self.oy)
        return point
        
    def pathd(self, d):
        #print d
        d = d.replace('M','')
        d1 =  d.replace('C',' ')
        d1 = d1.split(' ')
        l = len(d1)
        for i in range(0, len(d1)):
            d1[i] = self.point(d1[i]) #d1[1:l-1]
        return d1
     

def test():
    fn = "r1.svg" #sys.argv[1]
    svg = DotSVG(fn)
    #svg.svg()
    #svg.graph.dotGen()
    print svg.graph    
    
if __name__=='__main__':
    """
    def Usage():
        print >>sys.stderr, 'Usage %s [-n] filename' % sys.argv[0]
        sys.exit()
    if len(sys.argv)<2: Usage()
    nopp = sys.argv[1]=='-n'
    if nopp: del sys.argv[1]
    if len(sys.argv)<2: Usage()
    """
    test()
    #if not nopp: 
    #pprint.pprint(D[2][1][2])
    #pprint.pprint(D[2][1][2][3])
